package com.panghl;

import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import com.panghl.entity.User;
import com.panghl.repository.UserRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;

@SpringBootTest
@RunWith(SpringRunner.class)
public class SpringbootMongodbApplicationTests2 {

    @Autowired
    private MongoTemplate mongoTemplate;


    //添加操作
    @Test
    public void create(){
        User user = new User();
        user.setAge(20);
        user.setName("test");
        user.setEmail("phl@qq.com");
        mongoTemplate.insert(user);
    }

    //查询所有记录
    @Test
    public void findAll(){
        List<User> all = mongoTemplate.findAll(User.class);
        System.out.println(all);

    }

    //根据Id查询
    @Test
    public void findById(){
        User user = mongoTemplate.findById("60ea86f2cf40146eecfbcddd",User.class);
        System.out.println(user);
    }

    //条件查询
    @Test
    public void findUserList(){

        Query query = new Query(
                Criteria.where("name").is(1)
                        .and("age").is(17)
        );

        List<User> users = mongoTemplate.find(query, User.class);

        System.out.println(users);
    }


    //模糊查询
    @Test
    public void findLikeUserList(){
        //name like test
        String name = "est";
        String regex = String.format("%s%s%s","^.*",name,".*$");
        Pattern pattern = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
        Query query = new Query(
                Criteria.where("name").regex(pattern)
        );

        List<User> users = mongoTemplate.find(query, User.class);

        System.out.println(users);
    }

    //分页查询
    @Test
    public void findPageUserList(){
        int pageNo = 1;
        int pageSize = 3;
        String name = "est";
        //条件构建
        String regex = String.format("%s%s%s","^.*",name,".*$");
        Pattern pattern = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
        Query query = new Query(
                Criteria.where("name").regex(pattern)
        );

        //分页构建  --查询记录数
        long count = mongoTemplate.count(query, User.class);
        //分页
        List<User> users = mongoTemplate.find(query.skip((pageNo - 1) * 3).limit(pageSize), User.class);
        System.out.println("总共："+count+" ->数据："+users);
    }


    //修改
    @Test
    public void updateUser(){
        User user = mongoTemplate.findById("60ea86f2cf40146eecfbcddd", User.class);
        user.setEmail("phl0425@qq.com");
        Update update = new Update();
        update.set("name","test_1");
        update.set("email",user.getEmail());
        UpdateResult upsert = mongoTemplate.upsert(Query.query(Criteria.where("_id").is(user.getId()))
                , update, User.class);
        //影响行数
        long matchedCount = upsert.getMatchedCount();
        System.out.println("影响了："+matchedCount);
    }

    //删除操作
    @Test
    public void remove(){
        Query query = new Query(
                Criteria.where("_id").is("60ea86f2cf40146eecfbcddd")
        );
        DeleteResult deleteResult = mongoTemplate.remove(query, User.class);

        long deletedCount = deleteResult.getDeletedCount();

        System.out.println("删除影响行数:"+deletedCount+" 行");
    }

}
